package com.pzl.mapper.impl;

import com.pzl.mapper.RoleDao;
import com.pzl.entity.Role;
import com.pzl.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * RoleDaoImpl class
 *
 * @Author panzhenglei
 * @Date 2019/6/8 11:39 AM
 */

@Repository
public class RoleDaoImpl implements RoleDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public List<Role> findAll() {
        //select * from sys_role
        String sql = "select * from sys_role";

        List<Role> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Role.class));

        return query;
    }

    @Override
    public int save(String roleName, String roleDesc) {
        String sql = "INSERT INTO SYS_Role VALUES (null ,?,?)";

        int i = jdbcTemplate.update(sql, roleName,roleDesc);

        return i;
    }

    @Override
    public Long selectId() throws Exception{
        String sql = "select count(*) from sys_role ";
        Long role= jdbcTemplate.queryForObject(sql,Long.class);
        return role;
    }


    @Override
    public List<Role> selectRid(User user) throws Exception{
        String sql = "select * from sys_role WHERE ID IN (SELECT ROLEID FROM SYS_USER_ROLE WHERE USERID = ?)";
        List<Role> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Role.class), user.getId());
        return query;
    }


}
